package com.darrenchan.leetcode;

import java.util.Arrays;

/**
 * @Desc
 * @Author chenchi03
 * @CreateTime 2020-04-19 19:59
 */
public class QLint587 {
    public int twoSum6(int[] nums, int target) {
        if(nums == null || nums.length == 0) {
            return 0;
        }

        int i = 0, j = nums.length - 1;
        Arrays.sort(nums);
        int cnt = 0;
        while(i < j) {
            if(nums[i] + nums[j] == target) {
                cnt++;
                i++;
                j--;
                while(i < j && nums[i] == nums[i - 1]) {
                    i++;
                }
                while(i < j && nums[j] == nums[j + 1]) {
                    j--;
                }
            } else if(nums[i] + nums[j] < target) {
                i++;
            } else {
                j--;
            }
        }

        return cnt;
    }
}
